<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>http manual page - Tcl Bundled Packages</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> http</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<DL>
<DD><A HREF="http.htm#M2" NAME="L699">NAME</A>
<DL><DD>http &mdash; Client-side implementation of the HTTP/1.1 protocol</DD></DL>
<DD><A HREF="http.htm#M3" NAME="L700">SYNOPSIS</A>
<DL>
</DL>
<DD><A HREF="http.htm#M4" NAME="L701">DESCRIPTION</A>
<DD><A HREF="http.htm#M5" NAME="L702">COMMANDS</A>
<DL class="commands">
<DD><A HREF="http.htm#M6" NAME="L703"><B>::http::config</B> ?<I>options</I>?</A>
<DL class="commands">
<DD><A HREF="http.htm#M7" NAME="L704"><B>-accept</B> <I>mimetypes</I></A>
<DD><A HREF="http.htm#M8" NAME="L705"><B>-pipeline</B> <I>boolean</I></A>
<DD><A HREF="http.htm#M9" NAME="L706"><B>-postfresh</B> <I>boolean</I></A>
<DD><A HREF="http.htm#M10" NAME="L707"><B>-proxyhost</B> <I>hostname</I></A>
<DD><A HREF="http.htm#M11" NAME="L708"><B>-proxyport</B> <I>number</I></A>
<DD><A HREF="http.htm#M12" NAME="L709"><B>-proxyfilter</B> <I>command</I></A>
<DD><A HREF="http.htm#M13" NAME="L710"><B>-repost</B> <I>boolean</I></A>
<DD><A HREF="http.htm#M14" NAME="L711"><B>-urlencoding</B> <I>encoding</I></A>
<DD><A HREF="http.htm#M15" NAME="L712"><B>-useragent</B> <I>string</I></A>
<DD><A HREF="http.htm#M16" NAME="L713"><B>-zip</B> <I>boolean</I></A>
</DL>
<DD><A HREF="http.htm#M17" NAME="L714"><B>::http::geturl</B> <I>url</I> ?<I>options</I>?</A>
<DL class="commands">
<DD><A HREF="http.htm#M18" NAME="L715"><B>-binary</B> <I>boolean</I></A>
<DD><A HREF="http.htm#M19" NAME="L716"><B>-blocksize</B> <I>size</I></A>
<DD><A HREF="http.htm#M20" NAME="L717"><B>-channel</B> <I>name</I></A>
<DD><A HREF="http.htm#M21" NAME="L718"><B>-command</B> <I>callback</I></A>
<DD><A HREF="http.htm#M22" NAME="L719"><B>-handler</B> <I>callback</I></A>
<DD><A HREF="http.htm#M23" NAME="L720"><B>-headers</B> <I>keyvaluelist</I></A>
<DD><A HREF="http.htm#M24" NAME="L721"><B>-keepalive</B> <I>boolean</I></A>
<DD><A HREF="http.htm#M25" NAME="L722"><B>-method</B> <I>type</I></A>
<DD><A HREF="http.htm#M26" NAME="L723"><B>-myaddr</B> <I>address</I></A>
<DD><A HREF="http.htm#M27" NAME="L724"><B>-progress</B> <I>callback</I></A>
<DD><A HREF="http.htm#M28" NAME="L725"><B>-protocol</B> <I>version</I></A>
<DD><A HREF="http.htm#M29" NAME="L726"><B>-query</B> <I>query</I></A>
<DD><A HREF="http.htm#M30" NAME="L727"><B>-queryblocksize</B> <I>size</I></A>
<DD><A HREF="http.htm#M31" NAME="L728"><B>-querychannel</B> <I>channelID</I></A>
<DD><A HREF="http.htm#M32" NAME="L729"><B>-queryprogress</B> <I>callback</I></A>
<DD><A HREF="http.htm#M33" NAME="L730"><B>-strict</B> <I>boolean</I></A>
<DD><A HREF="http.htm#M34" NAME="L731"><B>-timeout</B> <I>milliseconds</I></A>
<DD><A HREF="http.htm#M35" NAME="L732"><B>-type</B> <I>mime-type</I></A>
<DD><A HREF="http.htm#M36" NAME="L733"><B>-validate</B> <I>boolean</I></A>
</DL>
<DD><A HREF="http.htm#M37" NAME="L734"><B>::http::formatQuery</B> <I>key value</I> ?<I>key value</I> ...?</A>
<DD><A HREF="http.htm#M38" NAME="L735"><B>::http::quoteString</B> <I>value</I></A>
<DD><A HREF="http.htm#M39" NAME="L736"><B>::http::reset</B> <I>token</I> ?<I>why</I>?</A>
<DD><A HREF="http.htm#M40" NAME="L737"><B>::http::wait</B> <I>token</I></A>
<DD><A HREF="http.htm#M41" NAME="L738"><B>::http::data</B> <I>token</I></A>
<DD><A HREF="http.htm#M42" NAME="L739"><B>::http::error</B> <I>token</I></A>
<DD><A HREF="http.htm#M43" NAME="L740"><B>::http::status</B> <I>token</I></A>
<DD><A HREF="http.htm#M44" NAME="L741"><B>::http::code</B> <I>token</I></A>
<DD><A HREF="http.htm#M45" NAME="L742"><B>::http::ncode</B> <I>token</I></A>
<DD><A HREF="http.htm#M46" NAME="L743"><B>::http::size</B> <I>token</I></A>
<DD><A HREF="http.htm#M47" NAME="L744"><B>::http::meta</B> <I>token</I></A>
<DD><A HREF="http.htm#M48" NAME="L745"><B>::http::cleanup</B> <I>token</I></A>
<DD><A HREF="http.htm#M49" NAME="L746"><B>::http::register</B> <I>proto port command</I></A>
<DD><A HREF="http.htm#M50" NAME="L747"><B>::http::registerError</B> <I>port</I> ?<I>message</I>?</A>
<DD><A HREF="http.htm#M51" NAME="L748"><B>::http::unregister</B> <I>proto</I></A>
</DL>
<DD><A HREF="http.htm#M52" NAME="L749">ERRORS</A>
<DL class="errors">
<DD><A HREF="http.htm#M53" NAME="L750"><B>ok</B></A>
<DD><A HREF="http.htm#M54" NAME="L751"><B>eof</B></A>
<DD><A HREF="http.htm#M55" NAME="L752"><B>error</B></A>
<DD><A HREF="http.htm#M56" NAME="L753"><B>timeout</B></A>
<DD><A HREF="http.htm#M57" NAME="L754"><B>reset</B></A>
</DL>
<DD><A HREF="http.htm#M58" NAME="L755">STATE ARRAY</A>
<DL class="state array">
<DD><A HREF="http.htm#M59" NAME="L756"><B>binary</B></A>
<DD><A HREF="http.htm#M60" NAME="L757"><B>body</B></A>
<DD><A HREF="http.htm#M61" NAME="L758"><B>charset</B></A>
<DD><A HREF="http.htm#M62" NAME="L759"><B>coding</B></A>
<DD><A HREF="http.htm#M63" NAME="L760"><B>currentsize</B></A>
<DD><A HREF="http.htm#M64" NAME="L761"><B>error</B></A>
<DD><A HREF="http.htm#M65" NAME="L762"><B>http</B></A>
<DD><A HREF="http.htm#M66" NAME="L763"><B>meta</B></A>
<DL class="state array">
<DD><A HREF="http.htm#M67" NAME="L764"><B>Content-Type</B></A>
<DD><A HREF="http.htm#M68" NAME="L765"><B>Content-Length</B></A>
<DD><A HREF="http.htm#M69" NAME="L766"><B>Location</B></A>
</DL>
<DD><A HREF="http.htm#M70" NAME="L767"><B>posterror</B></A>
<DD><A HREF="http.htm#M71" NAME="L768"><B>status</B></A>
<DD><A HREF="http.htm#M72" NAME="L769"><B>totalsize</B></A>
<DD><A HREF="http.htm#M73" NAME="L770"><B>type</B></A>
<DD><A HREF="http.htm#M74" NAME="L771"><B>url</B></A>
</DL>
<DD><A HREF="http.htm#M75" NAME="L772">PERSISTENT CONNECTIONS</A>
<DD><A HREF="http.htm#M76" NAME="L773">BASICS</A>
<DD><A HREF="http.htm#M77" NAME="L774">SPECIAL CASES</A>
<DD><A HREF="http.htm#M78" NAME="L775">EXAMPLE</A>
<DD><A HREF="http.htm#M79" NAME="L776">SEE ALSO</A>
<DD><A HREF="http.htm#M80" NAME="L777">KEYWORDS</A>
</DL>
<H3><A NAME="M2">NAME</A></H3>
http &mdash; Client-side implementation of the HTTP/1.1 protocol
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>package require http ?2.9?</B><BR>
<B>::http::config ?</B><I>-option value</I> ...?<BR>
<B>::http::geturl </B><I>url</I> ?<I>-option value</I> ...?<BR>
<B>::http::formatQuery</B> <I>key value</I> ?<I>key value</I> ...?<BR>
<B>::http::quoteString</B> <I>value</I><BR>
<B>::http::reset</B> <I>token</I> ?<I>why</I>?<BR>
<B>::http::wait </B><I>token</I><BR>
<B>::http::status </B><I>token</I><BR>
<B>::http::size </B><I>token</I><BR>
<B>::http::code </B><I>token</I><BR>
<B>::http::ncode </B><I>token</I><BR>
<B>::http::meta </B><I>token</I><BR>
<B>::http::data </B><I>token</I><BR>
<B>::http::error </B><I>token</I><BR>
<B>::http::cleanup </B><I>token</I><BR>
<B>::http::register </B><I>proto port command</I><BR>
<B>::http::registerError </B><I>port</I> ?<I>message</I>?<BR>
<B>::http::unregister </B><I>proto</I><BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
The <B>http</B> package provides the client side of the HTTP/1.1
protocol, as defined in RFC 7230 to RFC 7235, which supersede RFC 2616.
The package implements the GET, POST, and HEAD operations
of HTTP/1.1.  It allows configuration of a proxy host to get through
firewalls.  The package is compatible with the <B>Safesock</B> security
policy, so it can be used by untrusted applets to do URL fetching from
a restricted set of hosts. This package can be extended to support
additional HTTP transport protocols, such as HTTPS, by providing
a custom <B><A HREF="../TclCmd/socket.htm">socket</A></B> command, via <B>::http::register</B>.
<P>
The <B>::http::geturl</B> procedure does a HTTP transaction.
Its <I>options </I> determine whether a GET, POST, or HEAD transaction
is performed.
The return value of <B>::http::geturl</B> is a token for the transaction.
The value is also the name of an array in the ::http namespace
that contains state information about the transaction.  The elements
of this array are described in the <B><A HREF="#M58">STATE ARRAY</A></B> section.
<P>
If the <B>-command</B> option is specified, then
the HTTP operation is done in the background.
<B>::http::geturl</B> returns immediately after generating the
HTTP request and the callback is invoked
when the transaction completes.  For this to work, the Tcl event loop
must be active.  In Tk applications this is always true.  For pure-Tcl
applications, the caller can use <B>::http::wait</B> after calling
<B>::http::geturl</B> to start the event loop.
<P>
<B>Note:</B> The event queue is even used without the <B>-command</B> option.
As a side effect, arbitrary commands may be processed while <B>http::geturl</B> is running.
<H3><A NAME="M5">COMMANDS</A></H3>
<DL class="commands">
<DT><A NAME="M6"><B>::http::config</B> ?<I>options</I>?</A><DD>
The <B>::http::config</B> command is used to set and query the name of the
proxy server and port, and the User-Agent name used in the HTTP
requests.  If no options are specified, then the current configuration
is returned.  If a single argument is specified, then it should be one
of the flags described below.  In this case the current value of
that setting is returned.  Otherwise, the options should be a set of
flags and values that define the configuration:
<P>
<DL class="commands">
<DT><A NAME="M7"><B>-accept</B> <I>mimetypes</I></A><DD>
The Accept header of the request.  The default is */*, which means that
all types of documents are accepted.  Otherwise you can supply a
comma-separated list of mime type patterns that you are
willing to receive.  For example,
&ldquo;image/gif, image/jpeg, text/*&rdquo;.
<P><DT><A NAME="M8"><B>-pipeline</B> <I>boolean</I></A><DD>
Specifies whether HTTP/1.1 transactions on a persistent socket will be
pipelined.  See the <B>PERSISTENT SOCKETS</B> section for details. The default
is 1.
<P><DT><A NAME="M9"><B>-postfresh</B> <I>boolean</I></A><DD>
Specifies whether requests that use the <B>POST</B> method will always use a
fresh socket, overriding the <B>-keepalive</B> option of
command <B>http::geturl</B>.  See the <B>PERSISTENT SOCKETS</B> section for details.
The default is 0.
<P><DT><A NAME="M10"><B>-proxyhost</B> <I>hostname</I></A><DD>
The name of the proxy host, if any.  If this value is the
empty string, the URL host is contacted directly.
<P><DT><A NAME="M11"><B>-proxyport</B> <I>number</I></A><DD>
The proxy port number.
<P><DT><A NAME="M12"><B>-proxyfilter</B> <I>command</I></A><DD>
The command is a callback that is made during
<B>::http::geturl</B>
to determine if a proxy is required for a given host.  One argument, a
host name, is added to <I>command</I> when it is invoked.  If a proxy
is required, the callback should return a two-element list containing
the proxy server and proxy port.  Otherwise the filter should return
an empty list.  The default filter returns the values of the
<B>-proxyhost</B> and <B>-proxyport</B> settings if they are
non-empty.
<P><DT><A NAME="M13"><B>-repost</B> <I>boolean</I></A><DD>
Specifies what to do if a POST request over a persistent connection fails
because the server has half-closed the connection.  If boolean <B>true</B>, the
request
will be automatically retried; if boolean <B>false</B> it will not, and the
application
that uses <B>http::geturl</B> is expected to seek user confirmation before
retrying the POST.  The value <B>true</B> should be used only under certain
conditions. See the <B>PERSISTENT SOCKETS</B> section for details. The
default is 0.
<P><DT><A NAME="M14"><B>-urlencoding</B> <I>encoding</I></A><DD>
The <I>encoding</I> used for creating the x-url-encoded URLs with
<B>::http::formatQuery</B> and <B>::http::quoteString</B>.
The default is <B>utf-8</B>, as specified by RFC
2718.  Prior to http 2.5 this was unspecified, and that behavior can be
returned by specifying the empty string (<B>{}</B>), although
<I>iso8859-1</I> is recommended to restore similar behavior but without the
<B>::http::formatQuery</B> or <B>::http::quoteString</B>
throwing an error processing non-latin-1 characters.
<P><DT><A NAME="M15"><B>-useragent</B> <I>string</I></A><DD>
The value of the User-Agent header in the HTTP request.  In an unsafe
interpreter, the default value depends upon the operating system, and
the version numbers of <B>http</B> and <B><A HREF="../TclCmd/Tcl.htm">Tcl</A></B>, and is (for example)
&ldquo;<B>Mozilla/5.0 (Windows; U; Windows NT 10.0) http/2.9.0 Tcl/8.6.9</B>&rdquo;.
A safe interpreter cannot determine its operating system, and so the default
in a safe interpreter is to use a Windows 10 value with the current version
numbers of <B>http</B> and <B><A HREF="../TclCmd/Tcl.htm">Tcl</A></B>.
<P><DT><A NAME="M16"><B>-zip</B> <I>boolean</I></A><DD>
If the value is boolean <B>true</B>, then by default requests will send a header
&ldquo;<B>Accept-Encoding: gzip,deflate,compress</B>&rdquo;.
If the value is boolean <B>false</B>, then by default this header will not be sent.
In either case the default can be overridden for an individual request by
supplying a custom <B>Accept-Encoding</B> header in the <B>-headers</B> option
of <B>http::geturl</B>. The default is 1.
<P></DL>
<P><DT><A NAME="M17"><B>::http::geturl</B> <I>url</I> ?<I>options</I>?</A><DD>
The <B>::http::geturl</B> command is the main procedure in the package.
The <B>-query</B> option causes a POST operation and
the <B>-validate</B> option causes a HEAD operation;
otherwise, a GET operation is performed.  The <B>::http::geturl</B> command
returns a <I>token</I> value that can be used to get
information about the transaction.  See the <B><A HREF="#M58">STATE ARRAY</A></B> and
<B><A HREF="#M52">ERRORS</A></B> section for
details.  The <B>::http::geturl</B> command blocks until the operation
completes, unless the <B>-command</B> option specifies a callback
that is invoked when the HTTP transaction completes.
<B>::http::geturl</B> takes several options:
<P>
<DL class="commands">
<DT><A NAME="M18"><B>-binary</B> <I>boolean</I></A><DD>
Specifies whether to force interpreting the URL data as binary.  Normally
this is auto-detected (anything not beginning with a <B><A HREF="../TkCmd/text.htm">text</A></B> content
type or whose content encoding is <B>gzip</B> or <B>compress</B> is
considered binary data).
<P><DT><A NAME="M19"><B>-blocksize</B> <I>size</I></A><DD>
The block size used when reading the URL.
At most <I>size</I> bytes are read at once.  After each block, a call to the
<B>-progress</B> callback is made (if that option is specified).
<P><DT><A NAME="M20"><B>-channel</B> <I>name</I></A><DD>
Copy the URL contents to channel <I>name</I> instead of saving it in
<B>state(body)</B>.
<P><DT><A NAME="M21"><B>-command</B> <I>callback</I></A><DD>
Invoke <I>callback</I> after the HTTP transaction completes.
This option causes <B>::http::geturl</B> to return immediately.
The <I>callback</I> gets an additional argument that is the <I>token</I> returned
from <B>::http::geturl</B>. This token is the name of an array that is
described in the <B><A HREF="#M58">STATE ARRAY</A></B> section.  Here is a template for the
callback:
<P>
<PRE>proc httpCallback {token} {
    upvar #0 $token state
    # Access state as a Tcl array
}</PRE>
<P><DT><A NAME="M22"><B>-handler</B> <I>callback</I></A><DD>
Invoke <I>callback</I> whenever HTTP data is available; if present, nothing
else will be done with the HTTP data.  This procedure gets two additional
arguments: the socket for the HTTP data and the <I>token</I> returned from
<B>::http::geturl</B>.  The token is the name of a global array that is
described in the <B><A HREF="#M58">STATE ARRAY</A></B> section.  The procedure is expected
to return the number of bytes read from the socket.  Here is a
template for the callback:
<P>
<PRE>proc httpHandlerCallback {socket token} {
    upvar #0 $token state
    # Access socket, and state as a Tcl array
    # For example...
    ...
    set data [read $socket 1000]
    set nbytes [string length $data]
    ...
    return $nbytes
}</PRE>
<P>
The <B>http::geturl</B> code for the <B>-handler</B> option is not compatible with either compression or chunked transfer-encoding.  If <B>-handler</B> is specified, then to work around these issues <B>http::geturl</B> will reduce the HTTP protocol to 1.0, and override the <B>-zip</B> option (i.e. it will not send the header &quot;<B>Accept-Encoding: gzip,deflate,compress</B>&quot;).
<P>If options <B>-handler</B> and <B>-channel</B> are used together, the handler is responsible for copying the data from the HTTP socket to the specified channel.  The name of the channel is available to the handler as element <B>-channel</B> of the token array.
<P><DT><A NAME="M23"><B>-headers</B> <I>keyvaluelist</I></A><DD>
This option is used to add headers not already specified
by <B>::http::config</B> to the HTTP request.  The
<I>keyvaluelist</I> argument must be a list with an even number of
elements that alternate between keys and values.  The keys become
header field names.  Newlines are stripped from the values so the
header cannot be corrupted.  For example, if <I>keyvaluelist</I> is
<B>Pragma no-cache</B> then the following header is included in the
HTTP request:
<P>
<PRE>Pragma: no-cache</PRE>
<P><DT><A NAME="M24"><B>-keepalive</B> <I>boolean</I></A><DD>
If boolean <B>true</B>, attempt to keep the connection open for servicing
multiple requests.  Default is 0.
<P><DT><A NAME="M25"><B>-method</B> <I>type</I></A><DD>
Force the HTTP request method to <I>type</I>. <B>::http::geturl</B> will
auto-select GET, POST or HEAD based on other options, but this option
enables choices like PUT and DELETE for webdav support.
<P><DT><A NAME="M26"><B>-myaddr</B> <I>address</I></A><DD>
Pass an specific local address to the underlying <B><A HREF="../TclCmd/socket.htm">socket</A></B> call in case
multiple interfaces are available.
<P><DT><A NAME="M27"><B>-progress</B> <I>callback</I></A><DD>
The <I>callback</I> is made after each transfer of data from the URL.
The callback gets three additional arguments: the <I>token</I> from
<B>::http::geturl</B>, the expected total size of the contents from the
<B>Content-Length</B> meta-data, and the current number of bytes
transferred so far.  The expected total size may be unknown, in which
case zero is passed to the callback.  Here is a template for the
progress callback:
<P>
<PRE>proc httpProgress {token total current} {
    upvar #0 $token state
}</PRE>
<P><DT><A NAME="M28"><B>-protocol</B> <I>version</I></A><DD>
Select the HTTP protocol version to use. This should be 1.0 or 1.1 (the
default). Should only be necessary for servers that do not understand or
otherwise complain about HTTP/1.1.
<P><DT><A NAME="M29"><B>-query</B> <I>query</I></A><DD>
This flag causes <B>::http::geturl</B> to do a POST request that passes the
<I>query</I> as payload verbatim to the server.
The content format (and encoding) of <I>query</I> is announced by the header
field <B>content-type</B> set by the option <B>-type</B>.
<I>query</I> is an x-url-encoding formatted query, if used for html forms.
The <B>::http::formatQuery</B> procedure can be used to do the formatting.
<P><DT><A NAME="M30"><B>-queryblocksize</B> <I>size</I></A><DD>
The block size used when posting query data to the URL.
At most
<I>size</I>
bytes are written at once.  After each block, a call to the
<B>-queryprogress</B>
callback is made (if that option is specified).
<P><DT><A NAME="M31"><B>-querychannel</B> <I>channelID</I></A><DD>
This flag causes <B>::http::geturl</B> to do a POST request that passes the
data contained in <I>channelID</I> to the server. The data contained in
<I>channelID</I> must be an x-url-encoding
formatted query unless the <B>-type</B> option below is used.
If a Content-Length header is not specified via the <B>-headers</B> options,
<B>::http::geturl</B> attempts to determine the size of the post data
in order to create that header.  If it is
unable to determine the size, it returns an error.
<P><DT><A NAME="M32"><B>-queryprogress</B> <I>callback</I></A><DD>
The <I>callback</I> is made after each transfer of data to the URL
(i.e. POST) and acts exactly like the <B>-progress</B> option (the
callback format is the same).
<P><DT><A NAME="M33"><B>-strict</B> <I>boolean</I></A><DD>
Whether to enforce RFC 3986 URL validation on the request.  Default is 1.
<P><DT><A NAME="M34"><B>-timeout</B> <I>milliseconds</I></A><DD>
If <I>milliseconds</I> is non-zero, then <B>::http::geturl</B> sets up a timeout
to occur after the specified number of milliseconds.
A timeout results in a call to <B>::http::reset</B> and to
the <B>-command</B> callback, if specified.
The return value of <B>::http::status</B> is <B>timeout</B>
after a timeout has occurred.
<P><DT><A NAME="M35"><B>-type</B> <I>mime-type</I></A><DD>
Use <I>mime-type</I> as the <B>Content-Type</B> value, instead of the
default value (<B>application/x-www-form-urlencoded</B>) during a
POST operation.
<P><DT><A NAME="M36"><B>-validate</B> <I>boolean</I></A><DD>
If <I>boolean</I> is non-zero, then <B>::http::geturl</B> does an HTTP HEAD
request.  This request returns meta information about the URL, but the
contents are not returned.  The meta information is available in the
<B>state(meta) </B> variable after the transaction.  See the
<B><A HREF="#M58">STATE ARRAY</A></B> section for details.
<P></DL>
<P><DT><A NAME="M37"><B>::http::formatQuery</B> <I>key value</I> ?<I>key value</I> ...?</A><DD>
This procedure does x-url-encoding of query data.  It takes an even
number of arguments that are the keys and values of the query.  It
encodes the keys and values, and generates one string that has the
proper &amp; and = separators.  The result is suitable for the
<B>-query</B> value passed to <B>::http::geturl</B>.
<P><DT><A NAME="M38"><B>::http::quoteString</B> <I>value</I></A><DD>
This procedure does x-url-encoding of string.  It takes a single argument and
encodes it.
<P><DT><A NAME="M39"><B>::http::reset</B> <I>token</I> ?<I>why</I>?</A><DD>
This command resets the HTTP transaction identified by <I>token</I>, if any.
This sets the <B>state(status)</B> value to <I>why</I>, which defaults to
<B>reset</B>, and then calls the registered <B>-command</B> callback.
<P><DT><A NAME="M40"><B>::http::wait</B> <I>token</I></A><DD>
This is a convenience procedure that blocks and waits for the
transaction to complete.  This only works in trusted code because it
uses <B><A HREF="../TclCmd/vwait.htm">vwait</A></B>.  Also, it is not useful for the case where
<B>::http::geturl</B> is called <I>without</I> the <B>-command</B> option
because in this case the <B>::http::geturl</B> call does not return
until the HTTP transaction is complete, and thus there is nothing to
wait for.
<P><DT><A NAME="M41"><B>::http::data</B> <I>token</I></A><DD>
This is a convenience procedure that returns the <B>body</B> element
(i.e., the URL data) of the state array.
<P><DT><A NAME="M42"><B>::http::error</B> <I>token</I></A><DD>
This is a convenience procedure that returns the <B><A HREF="../TclCmd/error.htm">error</A></B> element
of the state array.
<P><DT><A NAME="M43"><B>::http::status</B> <I>token</I></A><DD>
This is a convenience procedure that returns the <B>status</B> element of
the state array.
<P><DT><A NAME="M44"><B>::http::code</B> <I>token</I></A><DD>
This is a convenience procedure that returns the <B>http</B> element of the
state array.
<P><DT><A NAME="M45"><B>::http::ncode</B> <I>token</I></A><DD>
This is a convenience procedure that returns just the numeric return
code (200, 404, etc.) from the <B>http</B> element of the state array.
<P><DT><A NAME="M46"><B>::http::size</B> <I>token</I></A><DD>
This is a convenience procedure that returns the <B>currentsize</B>
element of the state array, which represents the number of bytes
received from the URL in the <B>::http::geturl</B> call.
<P><DT><A NAME="M47"><B>::http::meta</B> <I>token</I></A><DD>
This is a convenience procedure that returns the <B>meta</B>
element of the state array which contains the HTTP response
headers. See below for an explanation of this element.
<P><DT><A NAME="M48"><B>::http::cleanup</B> <I>token</I></A><DD>
This procedure cleans up the state associated with the connection
identified by <I>token</I>.  After this call, the procedures
like <B>::http::data</B> cannot be used to get information
about the operation.  It is <I>strongly</I> recommended that you call
this function after you are done with a given HTTP request.  Not doing
so will result in memory not being freed, and if your app calls
<B>::http::geturl</B> enough times, the memory leak could cause a
performance hit...or worse.
<P><DT><A NAME="M49"><B>::http::register</B> <I>proto port command</I></A><DD>
This procedure allows one to provide custom HTTP transport types
such as HTTPS, by registering a prefix, the default port, and the
command to execute to create the Tcl <B>channel</B>. E.g.:
<P>
<PRE>package require http
package require tls

::http::register https 443 ::tls::socket

set token [::http::geturl https://my.secure.site/]</PRE>
<P><DT><A NAME="M50"><B>::http::registerError</B> <I>port</I> ?<I>message</I>?</A><DD>
This procedure allows a registered protocol handler to deliver an error
message for use by <B>http</B>.  Calling this command does not raise an
error. The command is useful when a registered protocol detects an problem
(for example, an invalid TLS certificate) that will cause an error to
propagate to <B>http</B>.  The command allows <B>http</B> to provide a
precise error message rather than a general one.  The command returns the
value provided by the last call with argument <I>message</I>, or the empty
string if no such call has been made.
<P><DT><A NAME="M51"><B>::http::unregister</B> <I>proto</I></A><DD>
This procedure unregisters a protocol handler that was previously
registered via <B>::http::register</B>, returning a two-item list of
the default port and handler command that was previously installed
(via <B>::http::register</B>) if there was such a handler, and an error if
there was no such handler.
<P></DL>
<H3><A NAME="M52">ERRORS</A></H3>
The <B>::http::geturl</B> procedure will raise errors in the following cases:
invalid command line options,
an invalid URL,
a URL on a non-existent host,
or a URL at a bad port on an existing host.
These errors mean that it
cannot even start the network transaction.
It will also raise an error if it gets an I/O error while
writing out the HTTP request header.
For synchronous <B>::http::geturl</B> calls (where <B>-command</B> is
not specified), it will raise an error if it gets an I/O error while
reading the HTTP reply headers or data.  Because <B>::http::geturl</B>
does not return a token in these cases, it does all the required
cleanup and there is no issue of your app having to call
<B>::http::cleanup</B>.
<P>
For asynchronous <B>::http::geturl</B> calls, all of the above error
situations apply, except that if there is any error while reading the
HTTP reply headers or data, no exception is thrown.  This is because
after writing the HTTP headers, <B>::http::geturl</B> returns, and the
rest of the HTTP transaction occurs in the background.  The command
callback can check if any error occurred during the read by calling
<B>::http::status</B> to check the status and if its <I>error</I>,
calling <B>::http::error</B> to get the error message.
<P>
Alternatively, if the main program flow reaches a point where it needs
to know the result of the asynchronous HTTP request, it can call
<B>::http::wait</B> and then check status and error, just as the
callback does.
<P>
In any case, you must still call
<B>::http::cleanup</B> to delete the state array when you are done.
<P>
There are other possible results of the HTTP transaction
determined by examining the status from <B>::http::status</B>.
These are described below.
<P>
<DL class="errors">
<DT><A NAME="M53"><B>ok</B></A><DD>
If the HTTP transaction completes entirely, then status will be <B>ok</B>.
However, you should still check the <B>::http::code</B> value to get
the HTTP status.  The <B>::http::ncode</B> procedure provides just
the numeric error (e.g., 200, 404 or 500) while the <B>::http::code</B>
procedure returns a value like
&ldquo;HTTP 404 File not found&rdquo;.
<P><DT><A NAME="M54"><B>eof</B></A><DD>
If the server closes the socket without replying, then no error
is raised, but the status of the transaction will be <B><A HREF="../TclCmd/eof.htm">eof</A></B>.
<P><DT><A NAME="M55"><B>error</B></A><DD>
The error message will also be stored in the <B><A HREF="../TclCmd/error.htm">error</A></B> status
array element, accessible via <B>::http::error</B>.
<P><DT><A NAME="M56"><B>timeout</B></A><DD>
A timeout occurred before the transaction could complete
<P><DT><A NAME="M57"><B>reset</B></A><DD>
user-reset
<P></DL>
<P>
Another error possibility is that <B>::http::geturl</B> is unable to
write all the post query data to the server before the server
responds and closes the socket.
The error message is saved in the <B>posterror</B> status array
element and then  <B>::http::geturl</B> attempts to complete the
transaction.
If it can read the server's response
it will end up with an <B>ok</B> status, otherwise it will have
an <B><A HREF="../TclCmd/eof.htm">eof</A></B> status.
<H3><A NAME="M58">STATE ARRAY</A></H3>
The <B>::http::geturl</B> procedure returns a <I>token</I> that can be used to
get to the state of the HTTP transaction in the form of a Tcl array.
Use this construct to create an easy-to-use array variable:
<P>
<PRE>upvar #0 $token state</PRE>
<P>
Once the data associated with the URL is no longer needed, the state
array should be unset to free up storage.
The <B>::http::cleanup</B> procedure is provided for that purpose.
The following elements of
the array are supported:
<P>
<DL class="state array">
<DT><A NAME="M59"><B>binary</B></A><DD>
This is boolean <B>true</B> if (after decoding any compression specified
by the
&ldquo;Content-Encoding&rdquo;
response header) the HTTP response is binary.  It is boolean <B>false</B>
if the HTTP response is text.
<P><DT><A NAME="M60"><B>body</B></A><DD>
The contents of the URL.  This will be empty if the <B>-channel</B>
option has been specified.  This value is returned by the <B>::http::data</B> command.
<P><DT><A NAME="M61"><B>charset</B></A><DD>
The value of the charset attribute from the <B>Content-Type</B> meta-data
value.  If none was specified, this defaults to the RFC standard
<B>iso8859-1</B>, or the value of <B>$::http::defaultCharset</B>.  Incoming
text data will be automatically converted from this charset to utf-8.
<P><DT><A NAME="M62"><B>coding</B></A><DD>
A copy of the <B>Content-Encoding</B> meta-data value.
<P><DT><A NAME="M63"><B>currentsize</B></A><DD>
The current number of bytes fetched from the URL.
This value is returned by the <B>::http::size</B> command.
<P><DT><A NAME="M64"><B>error</B></A><DD>
If defined, this is the error string seen when the HTTP transaction
was aborted.
<P><DT><A NAME="M65"><B>http</B></A><DD>
The HTTP status reply from the server.  This value
is returned by the <B>::http::code</B> command.  The format of this value is:
<P>
<PRE><I>HTTP/1.1 code string</I></PRE>
<P>The <I>code</I> is a three-digit number defined in the HTTP standard.
A code of 200 is OK.  Codes beginning with 4 or 5 indicate errors.
Codes beginning with 3 are redirection errors.  In this case the
<B>Location</B> meta-data specifies a new URL that contains the
requested information.
<P><DT><A NAME="M66"><B>meta</B></A><DD>
The HTTP protocol returns meta-data that describes the URL contents.
The <B>meta</B> element of the state array is a list of the keys and
values of the meta-data.  This is in a format useful for initializing
an array that just contains the meta-data:
<P>
<PRE>array set meta $state(meta)</PRE>
<P>
Some of the meta-data keys are listed below, but the HTTP standard defines
more, and servers are free to add their own.
<P>
<DL class="state array">
<DT><A NAME="M67"><B>Content-Type</B></A><DD>
The type of the URL contents.  Examples include <B>text/html</B>,
<B>image/gif,</B> <B>application/postscript</B> and
<B>application/x-tcl</B>.
<P><DT><A NAME="M68"><B>Content-Length</B></A><DD>
The advertised size of the contents.  The actual size obtained by
<B>::http::geturl</B> is available as <B>state(currentsize)</B>.
<P><DT><A NAME="M69"><B>Location</B></A><DD>
An alternate URL that contains the requested data.
<P></DL>
<P><DT><A NAME="M70"><B>posterror</B></A><DD>
The error, if any, that occurred while writing
the post query data to the server.
<P><DT><A NAME="M71"><B>status</B></A><DD>
See description in the chapter <B><A HREF="#M52">ERRORS</A></B> above for a
list and description of <B>status</B>.
During the transaction this value is the empty string.
<P><DT><A NAME="M72"><B>totalsize</B></A><DD>
A copy of the <B>Content-Length</B> meta-data value.
<P><DT><A NAME="M73"><B>type</B></A><DD>
A copy of the <B>Content-Type</B> meta-data value.
<P><DT><A NAME="M74"><B>url</B></A><DD>
The requested URL.
<P></DL>
<H3><A NAME="M75">PERSISTENT CONNECTIONS</A></H3>
<H4><A NAME="M76">BASICS</A></H4>
See RFC 7230 Sec 6, which supersedes RFC 2616 Sec 8.1.
<P>
A persistent connection allows multiple HTTP/1.1 transactions to be
carried over the same TCP connection.  Pipelining allows a
client to make multiple requests over a persistent connection without
waiting for each response.  The server sends responses in the same order
that the requests were received.
<P>
If a POST request fails to complete, typically user confirmation is
needed before sending the request again.  The user may wish to verify
whether the server was modified by the failed POST request, before
sending the same request again.
<P>
A HTTP request will use a persistent socket if the call to
<B>http::geturl</B> has the option <B>-keepalive true</B>. It will use
pipelining where permitted if the <B>http::config</B> option
<B>-pipeline</B> is boolean <B>true</B> (its default value).
<P>
The http package maintains no more than one persistent connection to each
server (i.e. each value of
&ldquo;domain:port&rdquo;).
If <B>http::geturl</B> is called to make a request over a persistent
connection while the connection is busy with another request, the new
request will be held in a queue until the connection is free.
<P>
The http package does not support HTTP/1.0 persistent connections
controlled by the <B>Keep-Alive</B> header.
<H4><A NAME="M77">SPECIAL CASES</A></H4>
This subsection discusses issues related to closure of the
persistent connection by the server, automatic retry of failed requests,
the special treatment necessary for POST requests, and the options for
dealing with these cases.
<P>
In accordance with RFC 7230, <B>http::geturl</B> does not pipeline
requests that use the POST method.  If a POST uses a persistent
connection and is not the first request on that connection,
<B>http::geturl</B> waits until it has received the response for the previous
request; or (if <B>http::config</B> option <B>-postfresh</B> is boolean <B>true</B>) it
uses a new connection for each POST.
<P>
If the server is processing a number of pipelined requests, and sends a
response header
&ldquo;<B>Connection: close</B>&rdquo;
with one of the responses (other than the last), then subsequent responses
are unfulfilled. <B>http::geturl</B> will send the unfulfilled requests again
over a new connection.
<P>
A difficulty arises when a HTTP client sends a request over a persistent
connection that has been idle for a while.  The HTTP server may
half-close an apparently idle connection while the client is sending a
request, but before the request arrives at the server: in this case (an
&ldquo;asynchronous close event&rdquo;)
the request will fail.  The difficulty arises because the client cannot
be certain whether the POST modified the state of the server.  For HEAD or
GET requests, <B>http::geturl</B> opens another connection and retransmits
the failed request. However, if the request was a POST, RFC 7230 forbids
automatic retry by default, suggesting either user confirmation, or
confirmation by user-agent software that has semantic understanding of
the application.  The <B>http::config</B> option <B>-repost</B> allows for
either possibility.
<P>
Asynchronous close events can occur only in a short interval of time.  The
<B>http</B> package monitors each persistent connection for closure by the
server.  Upon detection, the connection is also closed at the client end,
and subsequent requests will use a fresh connection.
<P>
If the <B>http::geturl</B> command is called with option <B>-keepalive true</B>,
then it will both try to use an existing persistent connection
(if one is available), and it will send the server a
&ldquo;<B>Connection: keep-alive</B>&rdquo;
request header asking to keep the connection open for future requests.
<P>
The <B>http::config</B> options <B>-pipeline</B>, <B>-postfresh</B>, and
<B>-repost</B> relate to persistent connections.
<P>
Option <B>-pipeline</B>, if boolean <B>true</B>, will pipeline GET and HEAD requests
made
over a persistent connection.  POST requests will not be pipelined - if the
POST is not the first transaction on the connection, its request will not
be sent until the previous response has finished.  GET and HEAD requests
made after a POST will not be sent until the POST response has been
delivered, and will not be sent if the POST fails.
<P>
Option <B>-postfresh</B>, if boolean <B>true</B>, will override the <B>http::geturl</B> option
<B>-keepalive</B>, and always open a fresh connection for a POST request.
<P>
Option <B>-repost</B>, if <B>true</B>, permits automatic retry of a POST request
that fails because it uses a persistent connection that the server has
half-closed (an
&ldquo;asynchronous close event&rdquo;).
Subsequent GET and HEAD requests in a failed pipeline will also be retried.
<I>The -repost option should be used only if the application understands
that the retry is appropriate</I> - specifically, the application must know
that if the failed POST successfully modified the state of the server, a repeat POST
would have no adverse effect.
<H3><A NAME="M78">EXAMPLE</A></H3>
This example creates a procedure to copy a URL to a file while printing a
progress meter, and prints the meta-data associated with the URL.
<P>
<PRE>proc httpcopy { url file {chunk 4096} } {
    set out [open $file w]
    set token [<B>::http::geturl</B> $url -channel $out &#92;
            -progress httpCopyProgress -blocksize $chunk]
    close $out

    # This ends the line started by httpCopyProgress
    puts stderr &quot;&quot;

    upvar #0 $token state
    set max 0
    foreach {name value} $state(meta) {
        if {[string length $name] &gt; $max} {
            set max [string length $name]
        }
        if {[regexp -nocase ^location$ $name]} {
            # Handle URL redirects
            puts stderr &quot;Location:$value&quot;
            return [httpcopy [string trim $value] $file $chunk]
        }
    }
    incr max
    foreach {name value} $state(meta) {
        puts [format &quot;%-*s %s&quot; $max $name: $value]
    }

    return $token
}
proc httpCopyProgress {args} {
    puts -nonewline stderr .
    flush stderr
}</PRE>
<H3><A NAME="M79">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/safe.htm">safe</A></B>, <B><A HREF="../TclCmd/socket.htm">socket</A></B>, <B>safesock</B>
<H3><A NAME="M80">KEYWORDS</A></H3>
<A href="../Keywords/I.htm#internet">internet</A>, <A href="../Keywords/S.htm#security policy">security policy</A>, <A href="../Keywords/S.htm#socket">socket</A>, <A href="../Keywords/W.htm#www">www</A>
<div class="copy">Copyright &copy; 1995-1997 Sun Microsystems, Inc.
<BR>Copyright &copy; 1998-2000 Ajuba Solutions.
<BR>Copyright &copy; 2004 ActiveState Corporation.
</div>
</BODY></HTML>
